//
//  LocalData.m
//  gamebox_qpp
//
//  Created by 赵隆杰 on 2021/12/10.
//

#import "LocalData.h"

@implementation LocalData

+ (YTKKeyValueStore *)fetchUserStore {
    return [self fetchValueStore:[self userTableName]];
}
+ (YTKKeyValueStore *)fetchValueStore:(NSString *)table {
    YTKKeyValueStore *store = [[YTKKeyValueStore alloc] initDBWithName:@"user.db"];
    [store createTableWithName:table];
    return store;
}

+ (NSString *)userTableName {
    return @"user_table";
}
+ (NSString *)tokenKey {
    return @"token";
}
+ (NSString *)userKey {
    return @"user";
}

///保存token
+ (void)saveToken:(NSString *)token
{
    [[self fetchUserStore] putString:token withId:[self tokenKey] intoTable:[self userTableName]];
}
///获取token
+ (NSString *)getToken
{
    NSString *string = nil;
    YTKKeyValueItem *item = [[self fetchUserStore] getYTKKeyValueItemById:[self tokenKey] fromTable:[self userTableName]];
    NSArray *result = (NSArray *)item.itemObject;
    if (result.count == 0) {
        return nil;
    }
    string = [[self fetchUserStore] getStringById:[self tokenKey] fromTable:[self userTableName]];
    return string;
}
///删除token
+ (void)deleteToken
{
    [[self fetchUserStore] deleteObjectById:[self tokenKey] fromTable:[self userTableName]];
}

#pragma mark - 用户登录信息
+ (void)saveUserInfoDic:(NSDictionary *)userInfoDic {
    NSMutableDictionary *temUserInfoDic = [NSMutableDictionary dictionaryWithDictionary:userInfoDic];
    //
    NSDictionary *dict = [[self fetchUserStore] getObjectById:[self userKey] fromTable:[self userTableName]];
    if (dict.allKeys.count) {
        for (NSString *key in dict.allKeys) {
            if (![temUserInfoDic containsObjectForKey:key]) {
                [temUserInfoDic setObject:dict[key] forKey:key];
            }
        }
        userInfoDic = [NSDictionary dictionaryWithDictionary:temUserInfoDic];
    }
    
    YTKKeyValueStore *store = [self fetchUserStore];
    [store putObject:userInfoDic withId:[self userKey] intoTable:[self userTableName]];
}

+ (UserInfoModel *)fetchUserInfo {
    NSDictionary *userInfoDic = [[self fetchUserStore] getObjectById:[self userKey] fromTable:[self userTableName]];
    UserInfoModel *userInfo = [UserInfoModel mj_objectWithKeyValues:userInfoDic];
    return userInfo;
}

+ (void)clearLoginInfo {
    
    [[self fetchUserStore] clearTable:[self userTableName]];
    [[self fetchHeadStore] clearTable:[self userHeadTableName]];
    [[self fetchShieldContentStore] clearTable:[self userShieldContentTableName]];
    [[self fetchShieldUserStore] clearTable:[self userShieldUserTableName]];
}

+ (NSString *)userHeadTableName {
    return @"user_head";
}
+ (YTKKeyValueStore *)fetchHeadStore {
    return [self fetchValueStore:[self userHeadTableName]];
}
///保存头像
+ (void)saveHeadStr:(NSString *)headStr
{
    NSString *userId = [self fetchUserInfo].userId;
    if (userId) {
        [[self fetchHeadStore] putString:headStr withId:userId intoTable:[self userHeadTableName]];
    }
    
}
///获取头像
+ (NSString *)getHeadStr
{
    NSString *string = nil;
    NSString *userId = [self fetchUserInfo].userId;
    if (userId) {
        YTKKeyValueItem *item = [[self fetchHeadStore] getYTKKeyValueItemById:userId fromTable:[self userHeadTableName]];
        NSArray *result = (NSArray *)item.itemObject;
        if (result.count == 0) {
            return nil;
        }
        string = [[self fetchHeadStore] getStringById:userId fromTable:[self userHeadTableName]];
    }
    return string;
}
///删除头像
+ (void)deleteHeadStr
{
    NSString *userId = [self fetchUserInfo].userId;
    if (userId) {
        [[self fetchHeadStore] deleteObjectById:userId fromTable:[self userHeadTableName]];
    }
    
}

+ (NSString *)userShieldContentTableName {
    return @"user_ShieldContent";
}
+ (YTKKeyValueStore *)fetchShieldContentStore {
    return [self fetchValueStore:[self userShieldContentTableName]];
}
+ (void)saveShieldContentWithGameId:(NSString *)gameId commentId:(NSString *)commentId
{
    NSArray *commentIds = [self getShieldContentWithGameId:gameId];
    BOOL isExist = NO;
    for (NSString *commentId_get in commentIds) {
        if ([commentId_get isEqualToString:commentId]) {
            isExist = YES;
            break;
        }
    }
    if (isExist) {
        return;
    }
    NSMutableArray *temArr = [NSMutableArray arrayWithArray:commentIds];
    [temArr addObject:commentId];
    YTKKeyValueStore *store = [self fetchShieldContentStore];
    [store putObject:temArr withId:gameId intoTable:[self userShieldContentTableName]];
}
+ (NSArray *)getShieldContentWithGameId:(NSString *)gameId
{
    NSArray *array = nil;
    YTKKeyValueItem *item = [[self fetchShieldContentStore] getYTKKeyValueItemById:gameId fromTable:[self userShieldContentTableName]];
    NSArray *result = (NSArray *)item.itemObject;
    if (result.count == 0) {
        return nil;
    }
    array = [[self fetchShieldContentStore] getObjectById:gameId fromTable:[self userShieldContentTableName]];
    return array;
}

+ (NSString *)userShieldUserTableName {
    return @"user_ShieldUser";
}
+ (YTKKeyValueStore *)fetchShieldUserStore {
    return [self fetchValueStore:[self userShieldUserTableName]];
}
+ (void)saveShieldUsersWithUserInfoDic:(NSDictionary *)userInfoDic
{
    NSArray *users = [self getShieldUsers];
    BOOL isExist = NO;
    for (NSDictionary *userInfoDic_get in users) {
        if ([userInfoDic_get[@"userId"] isEqualToString:userInfoDic[@"userId"]]) {
            isExist = YES;
            break;
        }
    }
    if (isExist) {
        return;
    }
    NSMutableArray *temArr = [NSMutableArray arrayWithArray:users];
    [temArr addObject:userInfoDic];
    YTKKeyValueStore *store = [self fetchShieldUserStore];
    [store putObject:temArr withId:[self fetchUserInfo].userId intoTable:[self userShieldUserTableName]];
}
+ (NSArray *)getShieldUsers
{
    NSArray *array = nil;
    YTKKeyValueItem *item = [[self fetchShieldUserStore] getYTKKeyValueItemById:[self fetchUserInfo].userId fromTable:[self userShieldUserTableName]];
    NSArray *result = (NSArray *)item.itemObject;
    if (result.count == 0) {
        return nil;
    }
    array = [[self fetchShieldUserStore] getObjectById:[self fetchUserInfo].userId fromTable:[self userShieldUserTableName]];
    return array;
}
+ (void)deleteShieldUser:(NSString *)userId
{
    NSArray *users = [self getShieldUsers];
    NSMutableArray *temArray = [NSMutableArray arrayWithArray:users];
    for (NSDictionary *userInfoDic_get in users) {
        if ([userInfoDic_get[@"userId"] isEqualToString:userId]) {
            [temArray removeObject:userInfoDic_get];
            break;
        }
    }
    YTKKeyValueStore *store = [self fetchShieldUserStore];
    [store putObject:temArray withId:[self fetchUserInfo].userId intoTable:[self userShieldUserTableName]];
    
}

+ (id)getMenudicWithKey:(NSString *)key
{
    NSBundle *bundlePath = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource: @"666Images" ofType :@"bundle"]];
    NSString *plistPath = [bundlePath pathForResource:@"Settings" ofType:@"plist"];
    NSDictionary *menuDic = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
    return menuDic[key];
}
    
@end
